<?php
class UsersClass {

	function UsersClass () {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}
	
	function permission_str($prior){
		if ($prior == NULL) $mod = "000000000000000";
		else {
				$mod = "";
				asort($prior);
				$prior = array_reverse($prior);
				for ($i=0; $i<$prior[0]; $i++) $mod .= "0";
				for ($i=0; $i<count($prior); $i++) {
					$mod = substr_replace($mod, "1", $prior[$i], 1);
				}
		}		
		return $mod;
	}
	
	function GetUserInfoAll($type){
		$rows = array(); $row3 = array();
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."users` WHERE type='".$type."'", __LINE__, __FILE__);		
		$num = mysql_num_rows($sql);
		if ($num > 0){
			while ($row1 = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$sql2 = $this->mysqlclass->mysql_my_query("select name, value FROM `".$this->setupdata['table_prefix']."users_data` WHERE parent=".$row1['id'], __LINE__, __FILE__);
				while ($row2 = mysql_fetch_array($sql2, MYSQL_ASSOC)) array_push($row3, $row2);
				for ($i=0; $i < count($row3); $i++)	$row4[$row3[$i]['name']] = $row3[$i]['value'];
				$row1['mod'] = preg_split('//', $row1['permis'], -1, PREG_SPLIT_NO_EMPTY);
				$row1 = array_merge($row1, $row4);
				unset($row1['permis']);
				array_push($rows, $row1);
			}
		}
		return $rows;
	}
	
	function GetUserInfo($id, $type){
		$row = array();
		if (intval($id) == 0) return $row;
		if ($type == "admins"){
			if ($id != "9999999"){
				$rows = array();
				$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."users` WHERE id=".$id." and type='".$type."'", __LINE__, __FILE__);		
				$num = mysql_num_rows($sql);
				if ($num > 0){
					$row = mysql_fetch_array($sql, MYSQL_ASSOC);
					$sql = $this->mysqlclass->mysql_my_query("select name, value FROM `".$this->setupdata['table_prefix']."users_data` WHERE parent=".$row['id'], __LINE__, __FILE__);		
					while ($row2 = mysql_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row2);
					$row['mod'] = preg_split('//', $row['permis'], -1, PREG_SPLIT_NO_EMPTY);
					$rows2 = array();
					for ($i=0; $i < count($rows); $i++)	$rows2[$rows[$i]['name']] = $rows[$i]['value'];
					$row = array_merge($row, $rows2);
//					$row['values'] = $rows2;
					unset($row['permis']);
				}
			} else {
				$mod = "111111111111111";
				$row['mod'] = preg_split('//', $mod, -1, PREG_SPLIT_NO_EMPTY);
			}
		} elseif ($type == "clients"){
			$sql = $this->mysqlclass->mysql_my_query("select id FROM `".$this->setupdata['table_prefix']."users` WHERE id=".$id." and type='".$type."'", __LINE__, __FILE__);		
			$num = mysql_num_rows($sql);
			if ($num > 0){
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."clients_data` WHERE parent=".$row['id'], __LINE__, __FILE__);		
				while ($row2 = mysql_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row2);
				$rows2 = array();
				for ($i=0; $i < count($rows); $i++)	$rows2[$rows[$i]['name']] = $rows[$i]['value'];
				$row = array_merge($row, $rows2);
//				$row['values'] = $rows2;
				unset($row['permis']);
			}
		}
		return $row;
	}
	
	function AddUserInfo($login, $pass, $type, $permis){
		$login = strtolower($login);
		$sql = $this->mysqlclass->mysql_my_query("select `id`, `type` FROM `".$this->setupdata['table_prefix']."users` WHERE login='".$login."'", __LINE__, __FILE__);
		$row = mysql_num_rows($sql);
		if ($row != 0) {
			$rows = mysql_fetch_array($sql, MYSQL_ASSOC);
			if (strpos($row['type'], "_") > 0) $result['error'] = "6";
			else {
				$sql = "DELETE FROM `".$this->setupdata['table_prefix']."users` WHERE `id`=".$rows['id'];
				$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
				$pass = md5($pass);
				$mod = $this->permission_str($permis);
				$sql = "INSERT INTO `".$this->setupdata['table_prefix']."users` (`login`,`pass`,`permis`,`type`) VALUES ('".$login."','".$pass."','".$mod."','".$type."')";
				$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
				$result['error'] = "";
				$result['parent'] = mysql_insert_id();
			}
		} else {
			$pass = md5($pass);
			$mod = $this->permission_str($permis);
			$sql = "INSERT INTO `".$this->setupdata['table_prefix']."users` (`login`,`pass`,`permis`,`type`) VALUES ('".$login."','".$pass."','".$mod."','".$type."')";
			$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
			$result['error'] = "";
			$result['parent'] = mysql_insert_id();
		}
		return $result;
	}
	
	function DelClientInfo($id){
		$m_version = mysql_get_server_info();
		if (($m_version !== false) && (strpos($m_version, "4.1.7") === false) && (strpos($m_version, "4.1.17") === false)){
			$sql = "UPDATE `".$this->setupdata['table_prefix']."users` SET `type`=CONCAT('del_', type) WHERE id = ".$id;
			$this->mysqlclass->mysql_my_query($sql);
		} else {
			$sql = "UPDATE `".$this->setupdata['table_prefix']."users` SET `type`='del_admins' WHERE id = ".$id." AND `type`='admins'";
			$this->mysqlclass->mysql_my_query($sql);
		}
	}
	
	function ModUserInfo($login_id, $login, $pass, $prior) {
		$login = strtolower($login);
		$mod = $this->permission_str($prior);
		if (strlen($pass) > 0) {
			$pass = md5($pass);
			$sql = "UPDATE `".$this->setupdata['table_prefix']."users` SET login='".$login."', pass='".$pass."', permis='".$mod."' WHERE id = ".$login_id;
			$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		} else {
			$sql = "UPDATE `".$this->setupdata['table_prefix']."users` SET login='".$login."', permis='".$mod."' WHERE id = ".$login_id;
			$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		}
	}
	
	function EditClientInfo($parent, $name, $value, $typ, $dat){
		foreach ($dat as $val) {
			if ($val['name'] == $name) {
				$type = $val['type_of_value'];
				$pos = $val['pos'];
				break;
			}
		}
		$sql = $this->mysqlclass->mysql_my_query("select id FROM `".$this->setupdata['table_prefix']."users_data` WHERE parent = ".$parent." and name='".$name."' and type='".$type."'", __LINE__, __FILE__);
		$row = mysql_num_rows($sql);
		if ($row == 0) {
			$sql = "INSERT INTO `".$this->setupdata['table_prefix']."users_data` (`parent`,`name`,`value`, `type`, `pos`) VALUES (".$parent.", '".$name."', '".$value."', '".$type."', ".$pos.")";
			$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		} else {
			$sql = "UPDATE `".$this->setupdata['table_prefix']."users_data` SET value='$value' WHERE parent = ".$parent." and name='".$name."'";
			$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		}
	}
}
?>
